package com.lion.appletrip;

import java.util.List;

import com.lion.appletrip.utils.BmobDataPrint;
import com.lion.appletrip.utils.PreferenceUtils;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class LoginLognActivity extends Activity
{

	private EditText edAccount;
	private EditText edMima;
	private int numAt;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_logn);
		Intent intent = getIntent();
		String hint = intent.getStringExtra("account");
		numAt = intent.getIntExtra("num", 0);
		edAccount = (EditText) findViewById(R.id.editText1_login_login);
		edMima = (EditText) findViewById(R.id.editText2_login_login);
		edAccount.setHint(hint);
		findViewById(R.id.btn_close_login_login).setOnClickListener(
		        new OnClickListener()
		        {

			        @Override
			        public void onClick(View v)
			        {
				        finish();
			        }
		        });
		findViewById(R.id.btn_regit_yes2).setOnClickListener(
		        new OnClickListener()
		        {

			        @Override
			        public void onClick(View v)
			        {
				        final String PhoneNum = edAccount.getText().toString();
				        final String PasWord = edMima.getText().toString();
				        // Bmob服务器登入
				        BmobQuery<BmobDataPrint> bmobQuery = new BmobQuery<BmobDataPrint>();
				        // Log.e("bmobQuery",
						// "bmobQuery:"+bmobQuery.toString());
				        bmobQuery.findObjects(LoginLognActivity.this,
				                new FindListener<BmobDataPrint>()
				                {
					                @Override
					                public void onSuccess(
					                        List<BmobDataPrint> objects)
					                {
						                if (objects != null
						                        && objects.size() > 0)
						                {
							                BmobDataPrint dataPrint = objects
							                        .get(objects.size() - 1);
							                String phoneNumber = dataPrint
							                        .getPhoneNumber();
							                String passWord = dataPrint
							                        .getPassWord();
							                if (!PhoneNum.equals(phoneNumber))
							                {
								                Toast.makeText(
								                        LoginLognActivity.this,
								                        "账号不对，请重新输入",
								                        Toast.LENGTH_SHORT)
								                        .show();
								                return;
							                }
							                if (!PasWord.equals(passWord))
							                {
								                Toast.makeText(
								                        LoginLognActivity.this,
								                        "密码错误，请重新输入",
								                        Toast.LENGTH_SHORT)
								                        .show();
								                return;
							                }
							                if (PhoneNum.equals(phoneNumber)
							                        && PasWord.equals(passWord))
							                {
							              
								                // 保存Bmob登入状态信息为true
								                PreferenceUtils.setBomb(
								                        LoginLognActivity.this,
								                        true);
								                PreferenceUtils.setLogin(
								                        LoginLognActivity.this,
								                        true);
								                Intent intent = new Intent();
								                intent.putExtra("Login", numAt);
								                intent.setClass(
								                        LoginLognActivity.this,
								                        MainActivity.class);
								                startActivity(intent);
								                finish();
							                }
						                }
					                }

					                @Override
					                public void onError(int arg0, String arg1)
					                {
						                Toast.makeText(LoginLognActivity.this,
						                        "该账号不存在", Toast.LENGTH_SHORT)
						                        .show();

					                }

				                });
			        }
		        });
	}

}
